package com.jiwei.system.controller;

import cn.yiban.open.FrameUtil;
import cn.yiban.open.Authorize;
import com.jiwei.system.pojo.MemberLogin;
import com.jiwei.system.utils.JWTUtil;
import com.jiwei.system.utils.CookieUtil;
import com.sun.org.apache.xerces.internal.util.SynchronizedSymbolTable;
import org.json.JSONObject;
import org.springframework.stereotype.Controller;
import com.jiwei.system.serviceImpl.MemberServiceImpl;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping(value = "member",method = RequestMethod.GET)
public class MemberPageController {
    @Resource(name = "MemberService")
    private MemberServiceImpl memberService;

    int i = 0;
    /**
     * 用户主页
     * @return ModelAmdView对象，对应templates的实体html
     */
//    @GetMapping("index")
//    public ModelAndView IndexPage(HttpServletRequest request, HttpServletResponse response) {
//        //加入权限判断，一共有四种不同的主页
//
//        String role = JWTUtil.getClaimNamedRole(CookieUtil.getCookieValueNamedRoleToken(request));
//        if (role == null) {
//            String token = CookieUtil.getCookieValueNamedToken(request);
//            String id = JWTUtil.getClaimNamedAccount(token);
//            role = memberService.getMemberRoleByID(id);
//            String roleToken = JWTUtil.createRoleToken(role);
//            CookieUtil.setRoleCookie(response, roleToken);
//        }
//        Map<String, String> roleMap = new HashMap<String, String>() {
//            {
//                put("队员", "pages/Member/index/index");
//                put("接线员", "pages/Operator/index/index");
//                put("管理员", "pages/Admin/index/index");
////                put("超管", "Admin/SuperAdminIndex");
//            }
//        };
////        System.out.print("成员权限是：");
////        System.out.println(role);
//        return new ModelAndView(roleMap.get(role));
//    }
    @GetMapping("index")
    public ModelAndView IndexPage(){ return new ModelAndView("pages/Member/index/index"); }

    @GetMapping("login")
    public ModelAndView loginPage(HttpServletResponse response,HttpServletRequest request) throws Exception {

//        FrameUtil util = new FrameUtil(request, response, "36321b8cbd2e6b15", "c484d43c629679689e61716c09d6b58b", "http://f.yiban.cn/iapp675285");
//        boolean reasult = util.perform();
        //        if(util.getAccessToken() == null){
//            System.out.print("acctoken");
//            System.out.println(util.getAccessToken());
//            Authorize au = new Authorize("36321b8cbd2e6b15", "c484d43c629679689e61716c09d6b58b");
//            String url = au.forwardurl("http://f.yiban.cn/iapp675285", "QUERY", Authorize.DISPLAY_TAG_T.WEB);
//            response.sendRedirect(url);
//            return new ModelAndView("pages/Member/member_login");
//        }

        return new ModelAndView("pages/Member/member_login");
    }

    /**
     * 用户个人信息页
     * @return ModelAmdView对象，对应templates的实体html
     */
    @GetMapping("personalInfoPage")
    public ModelAndView personalInformationPage(){
        return new ModelAndView("pages/Member/personalCenter/myInfo");
    }

    /**
     * 用户查看个人任务页
     * @return ModelAmdView对象，对应templates的实体html
     */
    @GetMapping("myTaskList")
    public ModelAndView memberTaskListPage(){
        return new ModelAndView("pages/Member/personalCenter/myWork");
    }

    /**
     * 用户修改密码页
     * @return ModelAmdView对象，对应templates的实体html
     */
    @GetMapping("changePassword")
    public ModelAndView changePasswordPage(){
        return new ModelAndView("pages/Member/personalCenter/changePassword");
    }

    @GetMapping("getWaitingReturnList")
    public ModelAndView returnThingPage(){
        return new ModelAndView("pages/Member/personalCenter/unreturnedItems");
    }

    @GetMapping("getCanBorrowItemList")
    public ModelAndView canBorrowItemPage(){
        return new ModelAndView("pages/Member/materials/materialsBorrowing");
    }

    @GetMapping("getCanBorrowBook")
    public ModelAndView canBorrowBookPage(){
        return new ModelAndView("pages/Member/materials/booksBorrowing");
    }

    @GetMapping("editor")
    public ModelAndView editorBlog(){
        return new ModelAndView("pages/Member/myBlogs/editor");
    }

    @GetMapping("quit")
    public String quit(HttpServletResponse response,HttpServletRequest request) throws IOException{
//        CookieUtil.removeMemberCookie(response,"author_token");
//        CookieUtil.removeMemberCookie(response,"role_token");
        request.getSession().removeAttribute("login");
        return "redirect:/member/login";
    }

}
